package com.qxzn.app.page.model;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.RobotDeviceListBean;
import com.qxzn.app.page.interfaces.MyInterface;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MineModel {

    private Context context;
    private MyInterface.GetRobotListListener getRobotListListener;
    private MyInterface.ChangeRobotListener changeRobotListener;
    private MyInterface.GetFamilyMemberListener getFamilyMemberListener;
    private MyInterface.AdminDelMemberListener adminDelMemberListener;
    private MyInterface.InvitationMemberListener invitationMemberListener;
    private MyInterface.AuthMemberListener authMemberListener;
    private MyInterface.AgreeInvitationListener agreeInvitationListener;

    public MineModel(Context context) {
        this.context = context;
    }

    /**
     * 获取已绑定的机器人列表
     */
    public void getRobotList() {
        HttpRequest.getService().getRobotDeviceList().enqueue(new Callback<RobotDeviceListBean>() {
            @Override
            public void onResponse(Call<RobotDeviceListBean> call, Response<RobotDeviceListBean> response) {
                RobotDeviceListBean baseBean = response.body();
                if (getRobotListListener != null) {
                    getRobotListListener.onGetRobotListSuccess(baseBean);
                }
            }

            @Override
            public void onFailure(Call<RobotDeviceListBean> call, Throwable t) {
                if (getRobotListListener != null) {
                    getRobotListListener.onGetRobotListFailure();
                }
            }
        });
    }

    /**
     * 用户切换机器人
     */
    public void changeRobot(JSONObject params, String device_id) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().changeRobot(body).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                BaseBean baseBean = response.body();
                if (changeRobotListener != null) {
                    changeRobotListener.onChangeRobotSuccess(baseBean, device_id);
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (changeRobotListener != null) {
                    changeRobotListener.onChangeRobotFailure();
                }
            }
        });
    }

    /**
     * 获取当前所有的用户（管理员可查未绑定用户）
     */
    public void getFamilyMember(JSONObject params) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().getFamilyMember(body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody responseBody = response.body();
                if (getFamilyMemberListener != null) {
                    getFamilyMemberListener.onGetFamilyMemberSuccess(responseBody);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                if (getFamilyMemberListener != null) {
                    getFamilyMemberListener.onGetFamilyMemberFailure();
                }
            }
        });
    }

    /**
     * 管理员删除该设备所绑定的用户
     */
    public void adminDelMember(JSONObject params) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().adminDelMember(body).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                if (adminDelMemberListener != null) {
                    adminDelMemberListener.onAdminDelMemberSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (adminDelMemberListener != null) {
                    adminDelMemberListener.onAdminDelMemberFailure();
                }
            }
        });
    }

    /**
     * 管理员邀请家庭成员
     */
    public void invitationMember(JSONObject params) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().invitationMember(body).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                if (invitationMemberListener != null) {
                    invitationMemberListener.onInvitationMemberSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (invitationMemberListener != null) {
                    invitationMemberListener.onInvitationMemberFailure();
                }
            }
        });
    }

    /**
     * 管理员处理用户申请绑定机器人
     */
    public void authMember(JSONObject params) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().authMember(body).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                if (authMemberListener != null) {
                    authMemberListener.onAuthMemberSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (authMemberListener != null) {
                    authMemberListener.onAuthMemberFailure();
                }
            }
        });
    }

    /**
     * 用户处理管理员的邀请
     */
    public void agreeInvitation(JSONObject params, int roles) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().agreeInvitation(body).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                if (agreeInvitationListener != null) {
                    agreeInvitationListener.onAgreeInvitationSuccess(response.body(), roles);
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (agreeInvitationListener != null) {
                    agreeInvitationListener.onAgreeInvitationFailure();
                }
            }
        });
    }

    /**
     * 在页面关闭时移除监听
     */
    public void removeListener() {
        this.getRobotListListener = null;
        this.changeRobotListener = null;
        this.getFamilyMemberListener = null;
        this.adminDelMemberListener = null;
        this.invitationMemberListener = null;
        this.authMemberListener = null;
        this.agreeInvitationListener = null;
    }

    public void setGetRobotListListener(MyInterface.GetRobotListListener getRobotListListener) {
        this.getRobotListListener = getRobotListListener;
    }

    public void setChangeRobotListener(MyInterface.ChangeRobotListener changeRobotListener) {
        this.changeRobotListener = changeRobotListener;
    }

    public void setGetFamilyMemberListener(MyInterface.GetFamilyMemberListener getFamilyMemberListener) {
        this.getFamilyMemberListener = getFamilyMemberListener;
    }

    public void setAdminDelMemberListener(MyInterface.AdminDelMemberListener adminDelMemberListener) {
        this.adminDelMemberListener = adminDelMemberListener;
    }

    public void setInvitationMemberListener(MyInterface.InvitationMemberListener invitationMemberListener) {
        this.invitationMemberListener = invitationMemberListener;
    }

    public void setAuthMemberListener(MyInterface.AuthMemberListener authMemberListener) {
        this.authMemberListener = authMemberListener;
    }

    public void setAgreeInvitationListener(MyInterface.AgreeInvitationListener agreeInvitationListener) {
        this.agreeInvitationListener = agreeInvitationListener;
    }
}
